package com.huawei.esdk.tp.northcommu.cxf.common.convert;

import java.util.ArrayList;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.platform.common.utils.DurationUtils;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.cxf.enumgen.FreeBusyState;
import com.huawei.esdk.tp.cxf.enumgen.FreeBusyStateMapping;
import com.huawei.esdk.tp.cxf.enumgen.SiteTypeMapping;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.SiteInfoCommService;
import com.huawei.esdk.tp.domain.model.bean.DateTimeRange;
import com.huawei.esdk.tp.domain.model.bean.FreeBusyInfo;
import com.huawei.esdk.tp.domain.model.bean.PagedList;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QueryAllSitesResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteByNameResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteByURIResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteFreeBusyResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteStatusResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySitesFreeBusyResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.SiteFreeBusyStates;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.SiteInfo;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.SitesStatusMap;

/** * @author w00208247 * * */
public class TPCommonSiteMgrConvert
{
    private SiteInfoCommService siteInfoCommService;

    /**
     * 查询所有会场SDKResult转换成北向response
     * @param result
     * @return QueryAllSitesResponse
     */
    public QueryAllSitesResponse sdkResult2QueryAllSitesResponse(
            SDKResult<PagedList<Site>> result)
    {
        QueryAllSitesResponse response = new QueryAllSitesResponse();
        response.setResultCode(Integer.parseInt(String.valueOf(result
                .getErrCode())));
        if (result.getResult().getRecords().size() > 0)
        {
            List<Site> records = result.getResult().getRecords();
            SiteInfo info = null;
            for (int index = 0; index < records.size(); index++)
            {
                info = new SiteInfo();
                info.setName(records.get(index).getName());
                info.setUri(records.get(index).getUri());
                info.setType(SiteTypeMapping.string2Int(records.get(index)
                        .getSiteType()));
                response.getSitesInfo().add(info);
            }
        }
        return response;
    }

    /**
     * 通过URI查询会场信息SDKResult转换成QuerySiteByURIResponse * @param result SDKResult
     * /** * @return QuerySiteByURIResponse
     */
    public QuerySiteByURIResponse sdkResult2QuerySiteByURIResponse(
            SDKResult<Site> result)
    {
        QuerySiteByURIResponse response = new QuerySiteByURIResponse();
        response.setResultCode(Integer.parseInt(String.valueOf(result
                .getErrCode())));
        if (null != result.getResult())
        {
            SiteInfo info = new SiteInfo();
            info.setName(result.getResult().getName());
            info.setUri(result.getResult().getUri());
            info.setType(SiteTypeMapping.string2Int(result.getResult()
                    .getSiteType()));
            response.setSiteInfo(info);
        }

        return response;
    }

    public QuerySiteByNameResponse sdkResult2QuerySiteByNameResponse(
            SDKResult<List<Site>> result)
    {
        QuerySiteByNameResponse response = new QuerySiteByNameResponse();
        response.setResultCode(Integer.parseInt(String.valueOf(result
                .getErrCode())));
        if (null != result.getResult() && result.getResult().size() > 0)
        {
            SiteInfo info = null;
            for (int i = 0; i < result.getResult().size(); i++)
            {
                info = new SiteInfo();
                info.setType(SiteTypeMapping.string2Int(result.getResult()
                        .get(i).getSiteType()));
                info.setName(result.getResult().get(i).getName());
                info.setUri(result.getResult().get(i).getUri());
                response.getSitesInfo().add(info);
            }
        }
        return response;
    }

    public QuerySiteFreeBusyResponse querySiteFreebusyModel2Soap(
            String siteUri, SDKResult<List<Site>> result) throws SDKException
    {
        //修改findBus,判空验证
        if(null == siteInfoCommService)
        {
            SDKException sdkException = new SDKException("siteInfoCommService is null");
            sdkException.setSdkErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            throw sdkException; 
        }
        
        QuerySiteFreeBusyResponse response = new QuerySiteFreeBusyResponse();
        response.setResultCode((int) result.getErrCode());
        if (0 == response.getResultCode())
        {
            if (isBusySite(siteUri, result.getResult()))
            {
                response.setState(FreeBusyStateMapping
                        .enum2Int(FreeBusyState.BUSY));
            }
            else if (siteInfoCommService.containSite(siteUri))
            {
                response.setState(FreeBusyStateMapping
                        .enum2Int(FreeBusyState.FREE));
            }
            else
            {
                response.setState(FreeBusyStateMapping
                        .enum2Int(FreeBusyState.INVAILD));
            }
        }
        return response;

    }

    public QuerySitesFreeBusyResponse sdkResult2QuerySitesFreebusyResponse(
            List<String> siteUrisList, SDKResult<List<Site>> result) throws SDKException
    {
        //修改findBus,判空验证
        if(null == siteInfoCommService)
        {
            SDKException sdkException = new SDKException("siteInfoCommService is null");
            sdkException.setSdkErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            throw sdkException; 
        }
        
        QuerySitesFreeBusyResponse response = new QuerySitesFreeBusyResponse();
        response.setResultCode((int) result.getErrCode());
        if (0 == response.getResultCode())
        {
            SitesStatusMap siteStatus = null;
            List<SitesStatusMap> sitesFreeBusyState = new ArrayList<SitesStatusMap>();
            if(null != siteUrisList)
            {
                for (String siteUri : siteUrisList)
                {
                    siteStatus = new SitesStatusMap();
                    siteStatus.setSiteUri(siteUri);
    
                    if (isBusySite(siteUri, result.getResult()))
                    {
                        siteStatus.setSiteStatus(FreeBusyStateMapping
                                .enum2Int(FreeBusyState.BUSY));
                    }
                    else if (siteInfoCommService.containSite(siteUri))
                    {
                        siteStatus.setSiteStatus(FreeBusyStateMapping
                                .enum2Int(FreeBusyState.FREE));
                    }
                    else
                    {
                        siteStatus.setSiteStatus(FreeBusyStateMapping
                                .enum2Int(FreeBusyState.INVAILD));
                    }
    
                    sitesFreeBusyState.add(siteStatus);
                }
            }
            response.getSitesFreeBusyState().addAll((sitesFreeBusyState));
        }
        return response;
    }

    private Boolean isBusySite(String Uri, List<Site> siteList)
    {
        if (null == Uri || null == siteList)
        {
            return false;
        }
        for (Site site : siteList)
        {
            if (Uri.equals(site.getUri())
                    && "BUSY".equals(site.getFreeBusyInfoList().get(0)
                            .getFreeBusyState()))
            {
                return true;
            }
        }
        return false;
    }
    public DateTimeRange querySiteStatusSoap2model(
        XMLGregorianCalendar beginTime, Integer duration)
    {
        if (null == beginTime || null == duration)
        {
            return null;
        }
        DateTimeRange timeRange = new DateTimeRange();
        //开始时间转换 减8小时
        timeRange.setStart(DateUtils.toDateSMC(beginTime));
        timeRange.setSpan(DurationUtils.durationInt2dur(duration).toString());
        return timeRange;
    }
    
    public QuerySiteStatusResponse querySiteStatusModel2soap(
        SDKResult<List<Site>> result)
    {
        QuerySiteStatusResponse response = new QuerySiteStatusResponse();
        response.setResultCode((int) result.getErrCode());
        if (0 == response.getResultCode())
        {
            SiteFreeBusyStates siteFreeBusyStates = null;
            com.huawei.esdk.tp.northcommu.cxf.common.gen.FreeBusyState freeBusyState = null;
            for (Site site : result.getResult())
            {
                siteFreeBusyStates = new SiteFreeBusyStates();
                siteFreeBusyStates.setUri(site.getUri());
    
                if (null != site.getFreeBusyInfoList())
                {
                    for (FreeBusyInfo freeBusyInfo : site.getFreeBusyInfoList())
                    {
                        freeBusyState = new com.huawei.esdk.tp.northcommu.cxf.common.gen.FreeBusyState();
                        freeBusyState.setConfId(freeBusyInfo
                                .getConfInternalId());
                        freeBusyState.setConfName(freeBusyInfo.getConfName());
                        // 会议开始时间转换 加8小时
                        freeBusyState.setStartTime(DateUtils
                                .toGregorianCalendarDatePlusTimezone(freeBusyInfo
                                        .getTimeRange().getStart()));
                        freeBusyState.setSpan(DurationUtils
                                .durationStr2int(freeBusyInfo.getTimeRange()
                                        .getSpan()));
                        freeBusyState.setState(FreeBusyStateMapping
                                .enum2Int(FreeBusyState.BUSY));
                        siteFreeBusyStates.getStates().add(freeBusyState);
                    }
                }
    
                response.getSiteFreeBusyStates().add(siteFreeBusyStates);
            }
    
        }
    
        return response;
    }
    
    public SiteInfoCommService getSiteInfoCommService()
    {
        return siteInfoCommService;
    }

    public void setSiteInfoCommService(SiteInfoCommService siteInfoCommService)
    {
        this.siteInfoCommService = siteInfoCommService;
    }

}
